package com.terrier.ui.controls
{
    import com.terrier.ui.core.WxContainer;
    import com.terrier.ui.events.WxRangeEvent;
    import com.terrier.ui.managers.WxPopUpManager;
    import com.terrier.ui.skins.IWxPanelSkin;
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.WxSkinManager;

    import flash.display.DisplayObject;
    import flash.geom.Rectangle;

    /**
     * 面板组件
     * @author Jarvis.weng
     */
    public class WxPanel extends WxContainer
    {
        /**
         * 构造函数
         */
        public function WxPanel()
        {
            super();
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getPanelSkin(skinID));
        }

        /**
         * @private
         */
        override public function setSkin(value:IWxSkinBase):void
        {
            if (skin != null && hasChild(skin.bgSkin))
            {
                removeChild(skin.bgSkin);
            }

            super.setSkin(value);

            invalidateView();
            invalidateLayout();
        }

        /**
         * 打开面板. Panel将被加入到PopUpManager中
         * @param reference Panel居中时的参照对象
         * @param center Panel是否要居中
         * @param modal 是否为模态窗口 (不能操作窗口以外区域的控件)
         * @param animate 是否具有开启效果的动画
         */
        public function open(reference:DisplayObject = null,
                             center:Boolean = true, modal:Boolean = false, animate:Boolean = true):void
        {
            updateView();
            updateLayout();
            WxPopUpManager.instance.addPopUp(this, null);
        }

        /**
         * 关闭面板. Panel将从PopUpManager中被移除
         * @param animate 是否具有关闭动画
         */
        public function close(animate:Boolean = true):void
        {
            WxPopUpManager.instance.removePopUp(this);
        }

        /**
         * @private
         */
        override protected function updateView():void
        {
            if (skin != null && skin.bgSkin != null)
            {
                addChildAt(skin.bgSkin, 0);
            }
        }

        private function get skin():IWxPanelSkin
        {
            return _skin as IWxPanelSkin;
        }
    }
}
